package jbl.service.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jbl.dao.TdaDaoI;
import jbl.dao.ThjDaoI;
import jbl.dao.TxgDaoI;
import jbl.model.Thj;
import jbl.model.Txg;
import jbl.pageModel.CountTda;
import jbl.pageModel.DataDaGrid;
import jbl.service.CountTdaServiceI;
import jbl.util.Message;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("countTdaService")
public class CountTdaServiceImpl implements CountTdaServiceI {


	
	private TdaDaoI daDao;

	private TxgDaoI xgDao;
	
	private ThjDaoI hjDao;
	
	@Autowired
	public void setXgDao(TxgDaoI xgDao) {
		this.xgDao = xgDao;
	}
	@Autowired
	public void setHjDao(ThjDaoI hjDao) {
		this.hjDao = hjDao;
	}
	
	
	@Autowired
	public void setdaDao(TdaDaoI daDao) {
		this.daDao = daDao;
	}

	private String addWhere(String hql,CountTda cTda, Map<String, Object> params){
		if(cTda.getPage() == 0){
			cTda.setPage(1);
		}
		if(null != cTda.getHjId() && !"".equals(cTda.getHjId())){
			hql += " and t1.numberID=:hjId ";
			params.put("hjId", cTda.getHjId());
		}
		return hql;
	}

	@Override
	public DataDaGrid getCountData(CountTda cTda) {
		DataDaGrid dg = new DataDaGrid();
		
		try {
			String hjHql = "from Thj t1 where 1=1 and t1.flag = 'T' ";
			Map<String, Object> params2 = new HashMap<String, Object>();
			hjHql = addWhere(hjHql, cTda , params2);
			String totalHql = "select count(*) "+ hjHql;
			hjHql += " order by t1.cjsj Desc";
			List<Thj> hjList = hjDao.find(hjHql, params2, cTda.getPage(), 1);
			if(null != hjList && hjList.size() > 0){
				Map<String, Object> params1 = new HashMap<String, Object>();
				String xgHql = "from Txg t where t.thj=:thj order by t.cjsj desc";
				params1.put("thj", hjList.get(0));
				List<Txg> xgList = xgDao.find(xgHql,params1);
				List<CountTda> countTdaList = new ArrayList<CountTda>();
				if(null != xgList && xgList.size() > 0){
					for(Txg xg : xgList){
						String daHql = "Select count(*) from Tda t where t.txg=:txg ";
						Map<String, Object> params = new HashMap<String, Object>();
						params.put("txg", xg);
						Long countNum = daDao.count(daHql,params);
						CountTda countTda = new CountTda();
						countTda.setCountNum(countNum);
						countTda.setXgId(xg.getId());
						countTda.setNormNum(toNormNum(countNum));
						
						countTdaList.add(countTda);
					}
					dg.setRows(countTdaList);
				}
				
			}
			dg.setObj(hjList.get(0));
			dg.setTotal(hjDao.count(totalHql,params2));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dg;
	}
	
	

	private long toNormNum(long countNum){
		if(countNum == 0){
			return 5;
		}else if(countNum < (Message.MAX_XG_NUM*0.25)){
			return 4;
		}else if(countNum < (Message.MAX_XG_NUM*0.5)){
			return 3;
		}else if(countNum < (Message.MAX_XG_NUM*0.75)){
			return 2;
		}else if(countNum <= Message.MAX_XG_NUM){
			return 1;
		}
		return 5;
	}
	
}
